package com.ele.client.activity.auth;

import com.github.gwtbootstrap.client.ui.AlertBlock;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.PasswordTextBox;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class RegistrationViewImpl extends Composite implements RegistrationView {

    private Presenter presenter;

    interface DefaultUiBinder extends UiBinder<Widget, RegistrationViewImpl> {}
    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    @UiField
    TextBox emailTextBox;
    @UiField
    PasswordTextBox passwordTextBox;
    @UiField
    Button signUpButton;
    @UiField
    AlertBlock alertBlock;
    @UiField
    PasswordTextBox confirmPasswordTextBox;

    public RegistrationViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void setEmail(String email) {
        emailTextBox.setValue(email);
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void startLoading() {
        signUpButton.state().loading();
    }

    @Override
    public void stopLoading() {
        signUpButton.state().reset();
    }

    @UiHandler("signUpButton")
    void onClick(ClickEvent event) {
        presenter.register(emailTextBox.getValue(), passwordTextBox.getValue(), confirmPasswordTextBox.getValue());
    }

    @UiHandler({"emailTextBox", "passwordTextBox", "confirmPasswordTextBox"})
    void onKeyDown(KeyDownEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            presenter.register(emailTextBox.getValue(), passwordTextBox.getValue(), confirmPasswordTextBox.getValue());
        }
    }
}
